# -*- coding: utf-8 -*-
from datetime import timedelta
from utils.operators.latest_only_spark_submit_operator import LatestOnlySparkSubmitOperator
from jms.time_sensor import time_after_07_10

jdbcUrl='{{ var.json.oracle_tab.url }}'
username='{{ var.json.oracle_tab.username }}'
password='{{ var.json.oracle_tab.password }}'
nowdt='{{ execution_date | date_add(1) | cst_ds }}'
nextdt='{{ execution_date | date_add(2) | cst_ds }}'
table="tab_barscan_collect"
env='{{ var.value.env_sync }}'

jsonpara="""{
"reader":{
"connect":{
"url":"jdbcUrlpara",
"username":"usernamepara",
"password":"passwordpara",
"driver":"oracle.jdbc.driver.OracleDriver"
},
"dbtype":"oracle",
"tableName":"tablepara",
"where":"inputtime>=to_date('nowdt 00:00:00','yyyy-mm-dd hh24:mi:ss') and inputtime<to_date('nowdt 07:30:00','yyyy-mm-dd hh24:mi:ss')",
"query":"",
"splitColumn":"billcode",
"equalitySectioning":1,
"containsnull":0,
"fetchsize":"1000",
"threadNumber":2
},
"channel":{
"filterAbnormalCharacter":0
},
"writer":{
"connect":{
"url":"jdbc:oracle:thin:@10.33.130.100:1521:orcl",
"username":"test_bi",
"password":"123456",
"driver":""
},
"dbtype":"hive",
"tableName":"tablepara",
"database":"jms_ods",
"writeMode": "overwrite",
"partitionColumn":"dt",
"partitionValue":"nowdt"},
"settting":{
"env":"envpara"}
}""".replace("jdbcUrlpara",jdbcUrl).replace("usernamepara",username).replace("passwordpara",password). \
    replace("nowdt",nowdt).replace("nextdt",nextdt). \
    replace("tablepara",table). \
    replace("envpara",env)

# from airflow.operators.dummy_operator import DummyOperator
# jms_ods__tab_barscan_collect__0730 = DummyOperator(
#     task_id='jms_ods__tab_barscan_collect__0730',
#     email='chenhongping@yl-scm.com',
#     retries=0,
#     priority_weight=0,
#     # sla=timedelta(hours=2),
# )

jms_ods__tab_barscan_collect__0730 = LatestOnlySparkSubmitOperator(
    task_id='jms_ods__tab_barscan_collect__0730',
    email=['chenhongping@yl-scm.com','yl_bigdata@yl-scm.com'],
    name='jms_ods__tab_barscan_collect__0730_{{ execution_date | date_add(1) | cst_ds }}',
    pool='oracle_tab',
    pool_slots=2,
    execution_timeout=timedelta(hours=2),
    driver_memory='2G',
    executor_memory='3G',
    executor_cores=2,
    num_executors=1,
    conf={'spark.dynamicAllocation.enabled'                  : 'true',
          'spark.shuffle.service.enabled'                    : 'true',
          'spark.dynamicAllocation.maxExecutors'             : 2,
          'spark.dynamicAllocation.cachedExecutorIdleTimeout': 30,
          'spark.sql.sources.partitionOverwriteMode'         : 'dynamic',
          'spark.executor.memoryOverhead'                    : '1G',
          },
    java_class='com.yunlu.bigdata.jobs.synchrotool.DataSynchDriver',  # spark 主类
    application='hdfs:///scheduler/jms/spark/sync/spark_sync.jar',  # spark jar 包
    application_args=[jsonpara,],
)

time_after_07_10 >> jms_ods__tab_barscan_collect__0730